//
//  ZGL24VideoVC.m
//  Faker
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGL24VideoVC.h"
#import "ZGL24VideoCell.h"
#import <MJRefresh.h>
#import "ZGLHTTPTOOL.h"
#import "ZGL24VideoModel.h"

@interface ZGL24VideoVC ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *modArr;
@property(nonatomic, assign)NSInteger cellheight;
@end

@implementation ZGL24VideoVC
static int count = 0;
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _modArr = [[NSMutableArray alloc]init];
        _cellheight = 44;
        [self createTableView];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _tableView.backgroundColor = backColor;
    self.view.backgroundColor = backColor;
}
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)createTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Width, Height-113) style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [self.tableView registerClass:[ZGL24VideoCell class] forCellReuseIdentifier:@"ZGL24VideoCell"];
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    __weak typeof(self) weakSelf = self;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        count = 0;
        [weakSelf handelDataWithCount:count];
    }];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        count += 10;
        [weakSelf handelDataWithCount:count];
    }];
    [_tableView.header beginRefreshing];
}

- (void)handelDataWithCount:(int)count{
    NSString *urlStr = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/home/%d-10.html", count];
    __weak typeof(self) weakSelf = self;
    [ZGLHTTPTOOL GETWithURL:urlStr withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        if (count == 0) {
            weakSelf.modArr = [[NSMutableArray alloc]initWithArray:[tempDic objectForKey:@"videoList"]];
        }else{
            [weakSelf.modArr addObjectsFromArray:[tempDic objectForKey:@"videoList"]];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    } withFail:^(NSError *error) {
        NSLog(@"没网:ZGL24VideoVC");
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGL24VideoModel *mod = [ZGL24VideoModel modWithDic:self.modArr[indexPath.row]];
    ZGL24VideoCell *cell = [[ZGL24VideoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ZGL24VideoCell"];
    cell.mod = mod;
    self.cellheight = cell.height;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellheight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.view.userInteractionEnabled = NO;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.view.userInteractionEnabled = YES;
}

- (void)dealloc{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
